package com.tedu.charging.service.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tedu.charging.service.user.dao.UserVehicleBindMapper;
import com.tedu.charging.service.user.dao.VehicleMapper;
import com.tedu.charging.service.user.pojo.param.VehicleBindParam;
import com.tedu.charging.service.user.pojo.po.UserVehicleBindPO;
import com.tedu.charging.service.user.pojo.po.VehiclePO;
import com.tedu.charging.common.pojo.vo.VehicleVO;
import com.tedu.charging.service.user.service.IUserVehicleBindService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* @author zjj
* @description 针对表【charging_user_vehicle_bind】的数据库操作Service实现
* @createDate 2023-08-29 17:52:18
*/
@Service
public class UserVehicleBindServiceImpl extends ServiceImpl<UserVehicleBindMapper, UserVehicleBindPO>
    implements IUserVehicleBindService {

    @Autowired
VehicleMapper vehicleMapper;

    @Autowired
    UserVehicleBindMapper userVehicleBindMapper;

    //根据用户编号查询车辆
    @Override
    public VehicleVO getVehicleByUserId(Integer userId) throws Throwable {
        VehiclePO vehiclePO = userVehicleBindMapper.selectVehicleByUserId(userId);
        if (ObjectUtils.isEmpty(vehiclePO)){
            throw  new Exception("获取车辆信息失败");
        }

        //vo中没有创建时间信息
        VehicleVO vehicleVO = new VehicleVO();
        BeanUtils.copyProperties(vehiclePO,vehicleVO);
        return vehicleVO;
    }

    //解绑
    @Override
    public boolean unBind(Integer userId,Integer vehicleId) throws Throwable {
        //更新条件
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId);
        updateWrapper.eq("vehicle_id",vehicleId);

        //更新数据
        UserVehicleBindPO userVehicleBindPO = new UserVehicleBindPO();
        userVehicleBindPO.setState(0);//0:解绑

        int updateRow = userVehicleBindMapper.update(userVehicleBindPO, updateWrapper);
        if (updateRow<1){
            throw new Exception("解绑失败");
        }
        return true;
    }

    //添加车辆
    @Override
    public Integer insertVehicleBind(VehicleBindParam vehicleBindParam) throws Throwable {

        VehiclePO vehiclePO=new VehiclePO();
        BeanUtils.copyProperties(vehicleBindParam,vehiclePO);
        int insertRow = vehicleMapper.insert(vehiclePO);
        if (insertRow<1){
            throw  new Exception("添加车辆失败");
        }
        //查询车辆id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("license",vehicleBindParam.getLicense());
        queryWrapper.eq("vin",vehicleBindParam.getVin());
        queryWrapper.last("limit 1");

        VehiclePO selectVehiclePO = vehicleMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(selectVehiclePO)){
            throw new Exception("添加车辆后，查询id失败");
        }
        Integer vehicleId = selectVehiclePO.getId();

        //添加绑定
        UserVehicleBindPO userVehicleBindPO = new UserVehicleBindPO();
        userVehicleBindPO.setUserId(vehicleBindParam.getUserId());
        userVehicleBindPO.setVehicleId(vehicleId);
        userVehicleBindPO.setState(1);//1:状态是绑定
        int insertBindRow = userVehicleBindMapper.insert(userVehicleBindPO);
        if (insertBindRow<1){
            throw new Exception("添加车辆绑定失败");
        }
        return vehicleId;
    }


}




